package main;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JSeparator;
import javax.swing.JTextField;
import javax.swing.border.EmptyBorder;

/**
 * GUI for launcher
 * 
 * @author DUY HAN PHAN
 * 
 */
public class MainFrame extends JFrame {

	private JPanel contentPane;
	private JTextField txtNumSellers;
	private JTextField txtNumBuyers;
	private JButton btnStartBuyers;
	private final JSeparator separator = new JSeparator();
	public static MainFrame mainFrame;
	private final RunTimeStarter runTimeStarter;
	public static volatile List<String> runningBuyerList = Collections.synchronizedList(new ArrayList<String>());
	public static volatile List<String> runningSellerList = Collections.synchronizedList(new ArrayList<String>());;

	public static boolean isRunningBuyer(String buyerName) {
		for (int i = 0; i < runningBuyerList.size(); i++) {
			if (runningBuyerList.get(i).equals(buyerName)) {
				return true;
			}
		}
		return false;
	}

	public static boolean isRunningSeller(String sellerName) {
		for (int i = 0; i < runningSellerList.size(); i++) {
			if (runningSellerList.get(i).equals(sellerName)) {
				return true;
			}
		}
		return false;
	}

	public static void main(String[] args) {
		// Show mainFrame
		mainFrame = new MainFrame();
		mainFrame.setVisible(true);
	}

	/**
	 * Create the frame.
	 */
	public MainFrame() {
		setResizable(false);
		runTimeStarter = new RunTimeStarter();

		setTitle("MainFrame");

		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 415, 177);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(null);

		JLabel lblNumberOfSeller = new JLabel("Number Of Seller Agents");
		lblNumberOfSeller.setBounds(10, 11, 158, 14);
		contentPane.add(lblNumberOfSeller);

		txtNumSellers = new JTextField();
		txtNumSellers.setBounds(178, 8, 86, 20);
		contentPane.add(txtNumSellers);
		txtNumSellers.setColumns(10);

		JButton btnStartSellers = new JButton("Start Sellers");
		btnStartSellers.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				int numSellers = 0;
				boolean isShowedMsgBox = false;
				try {
					numSellers = Integer.parseInt(txtNumSellers.getText());
				} catch (NumberFormatException ex) {
					ex.printStackTrace();
					isShowedMsgBox = true;
					// handling the wrong input from users
					JOptionPane.showMessageDialog(null, "Please input an integer number greater than 1", "InfoBox",
							JOptionPane.ERROR_MESSAGE);
				}
				if (numSellers < 1  && isShowedMsgBox == false) {
					JOptionPane.showMessageDialog(null, "Please input an integer number greater than 1", "InfoBox",
							JOptionPane.ERROR_MESSAGE);
					return;
				}
				runTimeStarter.startSellerAgents(numSellers);
			}
		});
		btnStartSellers.setBounds(275, 7, 115, 23);
		contentPane.add(btnStartSellers);

		JLabel lblNumberOfBuyer = new JLabel("Number Of Buyer Agents");
		lblNumberOfBuyer.setBounds(10, 54, 140, 14);
		contentPane.add(lblNumberOfBuyer);

		txtNumBuyers = new JTextField();
		txtNumBuyers.setBounds(178, 51, 86, 20);
		contentPane.add(txtNumBuyers);
		txtNumBuyers.setColumns(10);

		btnStartBuyers = new JButton("Start Buyers");
		btnStartBuyers.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				int numBuyers = 0;
				boolean isShowedMsgBox = false;
				try {
					numBuyers = Integer.parseInt(txtNumBuyers.getText());
				} catch (NumberFormatException ex) {
					ex.printStackTrace();
					isShowedMsgBox = true;
					// handling the wrong input from users
					JOptionPane.showMessageDialog(null, "Please input an integer number greater than 1", "InfoBox",
							JOptionPane.ERROR_MESSAGE);
				}
				if (numBuyers < 1 && isShowedMsgBox == false) {
					JOptionPane.showMessageDialog(null, "Please input an integer number greater than 1", "InfoBox",
							JOptionPane.ERROR_MESSAGE);
					return;
				}
				runTimeStarter.startBuyerAgents(numBuyers);
			}
		});
		btnStartBuyers.setBounds(275, 48, 115, 26);
		contentPane.add(btnStartBuyers);
		separator.setBounds(0, 80, 400, 14);
		contentPane.add(separator);

		JButton btnShowJadeGui = new JButton("Show JADE GUI");
		btnShowJadeGui.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				runTimeStarter.showJadeGui();
			}
		});
		btnShowJadeGui.setBounds(139, 98, 140, 26);
		contentPane.add(btnShowJadeGui);
	}
}
